<template>
  <div>
    <f-form ref='form' :action='api.payment' :form-key='this.formItem.paymentId'>
      <a-divider orientation='left'>收票单位信息</a-divider>
      <a-descriptions bordered size='small' :column='2'>
        <a-descriptions-item label='单位名称'>
          {{ supplierInfo.supplierName }}
        </a-descriptions-item>
        <a-descriptions-item label='纳税识别号'>
          {{ supplierInfo.taxNumber }}
        </a-descriptions-item>
        <a-descriptions-item label='开户行'>
          {{ supplierInfo.bankName }}
        </a-descriptions-item>
        <a-descriptions-item label='卡号'>
          {{ supplierInfo.bankNo }}
        </a-descriptions-item>
        <a-descriptions-item label='地址'>
          {{ supplierInfo.taxNumber }}
        </a-descriptions-item>
      </a-descriptions>
      <a-divider orientation='left'>开票信息</a-divider>
      <f-form-item label='开票单位' type='select' :required='true' name='invoiceUnitId' :value='formItem.invoiceUnitId' :data='api.invoiceunit.list' label-key='invoiceunitName' value-key='invoiceUnitId' @change='invoiceunitChange' />
      <f-form-item label='收款账户' v-show='loadBank' ref='bank' type='select' name='bankId' :value.sync='formItem.bankId' :data='api.bib.list' valueKey='bankId' label-key='bankName,bankNo' :required='true' :autoLoad='false' :dropdown-render='true' @addItem='addBank' />
      <f-form-item label='税金公式' name='amountFormulaContent' :required='true' :value.sync='formItem.amountFormulaContent' :slotObj='linkSolt' width='400px' @slotObjChange='slotObjChange' />
      <f-form-item label='发票类型' type='select' :required='true' data-type='number' name='invoiceType' :value.sync='formItem.invoiceType' :data='api.invoiceunit.invoiceType' @change='invoiceTypeChange' />
      <f-form-item label='应返金额' tooltip='如需要分批开票需拆分,如应返金额10000,拆分格式5000#5000,注意需要#分隔' name='yfSupplierRetable' :value.sync='formItem.supplierRetable' @change='invoiceTypeChange' />
      <f-form-item label='开票金额' name='invoiceAmount2' :value.sync='formItem.invoiceAmount' @change='invoiceTypeChange' />
      <f-form-item label='申请日期' type='date' name='amountDate' :required='true' :value='formItem.amountDate' />
      <f-form-item label='备注' name='remark' :value='formItem.remark' />
    </f-form>
    <a-modal title='添加银行账户' :visible='bankModal' width='600px' @cancel='bankCancel' @ok='bankSubmit'>
      <f-form ref='bankForm' :action='api.bib' :form-key='this.bankForm.bankId'>
        <f-form-item label='银行名称' name='bankName' :required='true' :value='bankForm.bankName' />
        <f-form-item label='银行卡号' name='bankNo' :required='true' :value='bankForm.bankNo' />
        <f-form-item label='收款人' name='linkMan' :required='true' :value='bankForm.linkMan' />
        <f-form-item label='联系方式' name='linkTel' :required='true' :value='bankForm.linkTel' />
        <f-form-item label='备注' name='remark' :value='bankForm.remark' />
      </f-form>
    </a-modal>
  </div>
</template>
<script>

import api from "./../js/api"

export default {
  name: "InvoiceForm",
  components: {},
  data() {
    let me = this
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {
        supplierId: ""
      },
      supplierRetable: 0,
      supplierInfo: {},
      loadBank: false,
      handlerName: "",
      selectIds: "",
      modalTitle: "开票申请确认",
      bankModal: false,
      params1: {
        formulaType: 5
      },
      bankForm: {},
      formItem: this.getFormItem()
    }
  },
  created() {
  },
  computed: {
    linkSolt: function() {
      return {
        name: "amountFormulaContent",
        slotName: "addonBefore",
        value: this.formItem.amountFormulaContent,
        width: 180,
        data: [{ label: "开票金额/1.03*0.0536", value: "开票金额/1.03*0.0536" }, { label: "开票金额/1.03*0.0435", value: "开票金额/1.03*0.0435" }]
      }
    }
  },
  methods: {
    getFormItem() {
      return {
        invoiceId: "",
        companyId: "",
        supplierId: "",
        invoiceTitle: "",
        organizationId: "",
        amountFormulaId: null,
        invoiceUnitId: null,
        amountFormulaContent: "",
        taxAmount: "",
        invoiceAmount: "",
        amountDate: "",
        bankId: null,
        invoiceType: null,
        userId: "",
        invoiceState: "",
        remark: ""
      }
    },
    setData(data) {
      if (data.supplierId) {
        this.formItem.supplierRetable = data.supplierRetable
        this.queryParam = Object.assign(this.queryParam, data)
        this.handlerName = "invoicePaymentSaveHandler"
        this.http.get(this, {
          url: this.api.supplierBank.getBank,
          params: {
            supplierId: data.supplierId,
            bankType: 2
          },
          success: function(me, data) {
            me.supplierInfo = data
          }
        })
      }
      if (data && data.paymentId) {
        this.queryParam = Object.assign(this.queryParam, data)
        this.handlerName = "invoicePaymentUpdateHandler"
        this.http.get(this, {
          url: this.api.payment.get,
          params: {
            paymentId: data.paymentId,
            handlerName: "invoicePaymentGetHandler"
          },
          success: function(me, data) {
            me.formItem = data
          }
        })
      }

    },
    slotObjChange(value) {
      this.formItem.amountFormulaContent = value
    },
    addBank() {
      if (!this.formItem.invoiceUnitId) {
        this.$message.error("请选择开票单位")
        return
      }
      this.bankModal = true
    },
    bankSubmit() {
      this.$refs.bankForm.submit({ invoiceUnitId: this.formItem.invoiceUnitId }, this.bankCancel)
    },
    bankCancel() {
      this.bankModal = false
      this.$refs["bank"].initData({ invoiceUnitId: this.formItem.invoiceUnitId, bakType: 1 })
    },
    //开票单位切换
    invoiceunitChange(value) {
      this.loadBank = true
      this.formItem.invoiceUnitId = value
      this.formItem.bankId = null
      this.$refs["bank"].initData({ invoiceUnitId: value, bakType: 1 })
    },
    handleSubmit() {
      this.queryParam.handlerName = this.handlerName
      this.$refs["form"].submit(this.queryParam, this.handleSuccess)
    },
    invoiceTypeChange() {
      if (this.formItem.supplierRetable) {
        let radio = 1
        let scale = 2
        if (this.formItem.invoiceType == 2) {
          radio = 1.03
          scale = 0
        }
        let supplierRetable = this.formItem.supplierRetable.split("#")

        var i = 0
        supplierRetable.forEach((item) => {
          if (i == 0) {
            this.formItem.invoiceAmount = (item * radio).toFixed(scale)
          } else {
            this.formItem.invoiceAmount += "#" + (item * radio).toFixed(scale)
          }
          i++
        })

      }
    },
    handleSuccess() {
      this.$emit("closeDrawer", false)
    }
  }
}
</script>
